#  -*- coding: UTF-8 -*-
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

class Newreport():
    def Getnewrrport(self):
        file_address='./report'
        Lists =os.listdir('file')
        Lists.sort(lambda fn:os.path.getmtime(file_address+'\\' +fn))
        new_report =os.path.join(file_address,Lists[-1])
        f=open(new_report,'r')
        new_report_content=f.read()
        f.close()
        yield new_report_content
        yield new_report



class Send_mail():

    def sendMail(self):
        msg=MIMEMultipart()
        smtplib_server='smtp.qq.com'
        sender ='1457252030@qq.com'
        passworld='saaaaaaaa'
        receivers ='xxxx@qq.com'
        mail_msg ='这个是邮件njjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj的测试内容'
        msg.attach(MIMEText(mail_msg,'html','utf-8'))  # 邮件内容一
        msg['from']=Header('追风<%s>'% sender,'UTF-8')
        msg['TO'] = Header('<%s>' % receivers, 'UTF-8')
        subject='测试邮件标题'
        msg['subject']=Header('subject','UTF-8')
        mail_msg2 = Newreport.Getnewrrport().next()  # 邮件内容二
        msg.attach(MIMEText(mail_msg2,'html','utf-8'))
        send_file= Newreport.Getnewrrport().next()

        att=(MIMEText(send_file,'base64','utf-8'))
        att['Content-Type']='application/octet-stream'
        att['Content-Disposition'] = "attachment;filename='report.txt'"
        att.attach()









        try:
            smpt=smtplib.SMTP_SSL(smtplib_server,456)
            smpt.login(sender,passworld)
            smpt.sendmail(sender,receivers,msg.as_string())
            print "邮件发送成功"
        except smtplib.SMTPException ,e:
            print "Error: 无法发送邮件"